//
//  RootViewController.m
//  相册
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 YYL. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollview.tag = 1001;
    scrollview.delegate = self;
    
    for (int i = 1; i < 11; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollview.frame.size.width, scrollview.frame.size.height)];
        UIScrollView *little = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width * (i - 1), 0, imageView.frame.size.width, imageView.frame.size.height)];
        
        if (i == 1) {
             imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"8"]];
            [little addSubview:imageView];
        } else if (i == 10) {
             imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"1"]];
            [little addSubview:imageView];
        } else {
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i - 1]];
            [little addSubview:imageView];
        }
        
//        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i]];
        
        little.delegate = self;
        
        little.minimumZoomScale = 0.5;
        little.maximumZoomScale = 2.0;
        
        [little addSubview:imageView];
        [scrollview addSubview:little];
        [little release];
        [imageView release];
    }
    
    scrollview.contentSize = CGSizeMake(scrollview.frame.size.width * 10, 0);
    
    scrollview.showsHorizontalScrollIndicator= NO;
    scrollview.pagingEnabled = YES;
    scrollview.bounces = NO;

    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, scrollview.frame.size.width, 20)];
    pageControl.tag = 1002;
    
    pageControl.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:0.5];
    pageControl.numberOfPages = 8;
    
    
    
    [pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    
    
    scrollview.contentOffset = CGPointMake(scrollview.frame.size.width, 0);
    [self.view addSubview:scrollview];
    [scrollview release];
    [self.view addSubview:pageControl];
    [pageControl release];
    

}

#pragma mark - 获取缩放视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}

#pragma mark - PageControl方法

- (void)pageControlAction:(UIPageControl *)pageControl {
    
    UIScrollView *scrollView = [self.view viewWithTag:1001];
    
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * (pageControl.currentPage + 1), 0) animated:YES];
    
    for (UIScrollView *little in scrollView.subviews) {
        
        if ([little isKindOfClass:[UIScrollView class]]) {
            little.zoomScale = 1;
        }
        
    }
    if (pageControl.currentPage == 7) {
        scrollView.contentOffset = CGPointMake(0, 0);
    }
    if (pageControl.currentPage == 0) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * 7, 0);
    }
    
    
}

#pragma mark - 滑动图片改变pageControl

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    UIPageControl *pageControl = [self.view viewWithTag:1002];
    
    pageControl.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
    
    if (scrollView.contentOffset.x / self.view.frame.size.width == 0) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * 8, 0);
    }
    if (scrollView.contentOffset.x / self.view.frame.size.width == 9) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
    }
    
    
    
    
    for (UIScrollView *little in scrollView.subviews) {
        if ([little isKindOfClass:[UIScrollView class]]) {
            little.zoomScale = 1;
            
        }
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
